extends Polygon2D

var margin = 100
var lower_bound = 800
var Grid = null
var sea_level = 0
onready var vp_size = get_viewport().size

# Called when the node enters the scene tree for the first time.
func _ready():
	Grid = $"../GRID"
	sea_level = Grid.position.y + Grid.GROUND_LINE*Grid.BLOCKSIZE
	print (sea_level)
	generate_stars(Vector2(0,0),Vector2(get_viewport().size.x,get_viewport().size.y),120)

func generate_stars(bb_vec1,bb_vec2,n):
	print("generating stars")
	randomize()
	for i in range(n):
		var poly = create_polygon(0, 3, 5, ColorN('white'))
		poly.position = Vector2(rand_range(bb_vec1.x,bb_vec2.x),rand_range(bb_vec1.y,bb_vec2.y))
		poly.rotation = rand_range(0,2*PI)
		var sc = rand_range(0,1)
		poly.scale = Vector2(sc,sc)
		add_child(poly)

func create_polygon(z,steps,rad=100, col=ColorN("cyan",1), startang=0,finishang=PI*2):
	#print ("creating a star\n")
	if startang==finishang:
		return
	var anglestep=(finishang-startang)/steps
	#centerpoint
	var varang=startang
	var pool=PoolVector2Array()
	pool.push_back(Vector2(0,0))
	#arch
	while (varang<=finishang):
		pool.push_back(rad*Vector2(cos(varang),sin(varang)))
		varang=varang+anglestep
	varang=finishang
	pool.push_back(rad*Vector2(cos(varang),sin(varang)))
	var Poly=Polygon2D.new()
	Poly.color=col
	Poly.polygon=pool
	var outline=Line2D.new()
	pool.remove(0)
	outline.points=pool
	outline.default_color=Poly.color.lightened(0.5)
	outline.width=1.5
	Poly.z_index=z
	outline.z_index=z
	Poly.add_child(outline)
	return Poly
